package com.pro.system.Controller;

import com.pro.system.Repository.MovieDetailRepository;

import com.pro.system.entity.Movie;
import com.pro.system.entity.MovieDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("admin/moviedetails")
public class MovieDetailController {


    @Autowired
    MovieDetailRepository movieDetailRepository;

    @GetMapping
    public String showMovieDetail (Model model){
        List<MovieDetail> movieDetail = movieDetailRepository.findAll();
        model.addAttribute("movieDetail",movieDetail);
        return "web/background/moviedetails/moviedetails";
    }

    @GetMapping("intonew")
    public String intonew (Model model){
        MovieDetail movieDetail = new MovieDetail();
        model.addAttribute(movieDetail);
        return "web/background/moviedetails/moviedetails-new";
    }
    @PostMapping("save")
    public String save(Model model,MovieDetail movieDetail){
        MovieDetail movieDetail1 = new MovieDetail();
        model.addAttribute(movieDetail1);
        MovieDetail save = movieDetailRepository.save(movieDetail);
        if(save != null){
            return "web/background/moviedetails/moviedetails-new";
        }
        else {
            return "";
        }
    }

    @PostMapping("deleteItemByCheck")
    public String deleteItemByCheck(Model model, Integer[] getvalues){
        for(int i=0; i<getvalues.length;++i){
            movieDetailRepository.deleteById(getvalues[i]);
        }
        return "redirect:/admin/moviedetails";
    }

    @PostMapping("research")
    public String research(Model model,@RequestParam String byNameLike){
        List<MovieDetail> byNameLikes = movieDetailRepository.findByNameLike(byNameLike);
        model.addAttribute("movieDetail",byNameLikes);
        return  "web/background/moviedetails/moviedetails";
    }
}
